<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\model\contract;

use app\helper\TempData;
use app\common\model\BaseModel;
use app\common\model\users\UsersModel;
use app\common\model\room\RoomBasicModel;
use app\common\model\bill\BillCateModel;

class ContractBasicModel extends BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    public static function tablePk(): string
    {
        return 'id';
    }

    public static function tableName(): string
    {
        return 'contract_basic';
    }

    /**
     * 合同倒计时天数
     * @return int
     *
     * @author Amos
     * 2024-05-12 22:26
     */
    public function getLaveDaysAttr()
    {
        return intval((strtotime($this->end_date) - strtotime(date('ymd'))) / 86400);
    }

    public function userInfo()
    {
        return $this->hasOne(UsersModel::class, 'id', 'user_id');
    }

    public function pictureInfo()
    {
        return $this->hasMany(ContractPictureModel::class, 'contract_id', 'id');
    }

    public function getPictureAttr()
    {
        return get_arr_column($this->pictureInfo()->field('image_url')->select()->toArray(), 'image_url');
    }


    public function roomInfo()
    {
        return $this->hasOne(RoomBasicModel::class, 'id', 'room_id');
    }

    public function getContractRentAttr()
    {
        return $this->hasOne(ContractBillModel::class, 'contract_id', 'id')->where('if_bill', 2)->value('amount');
    }

    public function getContractDepositAttr()
    {
        return $this->hasOne(ContractBillModel::class, 'contract_id', 'id')->where('if_bill', 1)->value('amount');
    }


}
